//
//  BudgetViewController.m
//  预购清单
//
//  Created by  on 2022/10/16.
//

#import "BudgetViewController.h"
#import "BudgetCell.h"
#import "PayCell.h"
#import "InputAlertController.h"

@interface BudgetViewController ()
@property (nonatomic,strong) NSDateComponents *date;
@property (nonatomic,strong) UIButton *timeBtn;
@property (nonatomic,assign) NSInteger stamp;
@property (nonatomic, strong) Budget *model;

@end

@implementation BudgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self addNotify];
}
- (void)initUI
{
    self.timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.timeBtn.bounds = CGRectMake(0, 0, 200, 30);
    [self.timeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.timeBtn setImage:[UIImage imageNamed:@"更多-下拉-黑"] forState:UIControlStateNormal];
    [self.timeBtn addTarget:self action:@selector(actionForMore) forControlEvents:UIControlEventTouchUpInside];
    self.timeBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -40, 0, 0);
    self.timeBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -150);
    self.navigationItem.titleView = self.timeBtn;
    self.stamp = [[NSDate currentTimeStamp:@"yyyy-MM"] intValue];
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.layer.masksToBounds = NO;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[BudgetCell nib] forCellReuseIdentifier:[BudgetCell ID]];
    [self.tableView registerNib:[PayCell nib] forCellReuseIdentifier:[PayCell ID]];
    self.tableView.c_top.equalTo(self.view.c_top).coffset(16);
    self.tableView.c_left.equalTo(self.view.c_left).coffset(0);
    self.tableView.c_right.equalTo(self.view.c_right).coffset(0);
    self.tableView.c_bottom.equalTo(self.view.c_bottom).coffset(0);
}
- (void)actionForMore{
    [self presend:LS(@"更多")];
}
- (void)presend:(NSString *)title{
    WS(self)
    if ([title isEqualToString:LS(@"更多")]) {
        PickViewController *vc = [PickViewController new];
        vc.stamp = [[NSDate currentTimeStamp:@"yyyy-MM"] intValue];;
        vc.stampBlock = ^(NSInteger stamp) {
            weakself.stamp = stamp;
        };
        [vc show];
    }else if ([title isEqualToString:LS(@"编辑预算")]){
        SheetViewController *vc = [SheetViewController new];
        vc.withCancel = NO;
        vc.array = @[@"编辑总预算",@"清除总预算"];
        vc.block = ^(int index, NSString * _Nonnull text) {
                [weakself presend:LS(text)];
        };
        [weakself presentViewController:vc animated:NO completion:nil];
    }else if ([title isEqualToString:LS(@"编辑总预算")]){
        InputAlertController *vc = [InputAlertController new];
        vc.block = ^(NSString * _Nonnull str) {
            weakself.model.total = [str floatValue];
            [BudgetTool update:weakself.model complete:^(BOOL success) {
                [NotifyTool post:notifyBudgetUpdate :nil];
            }];
            [weakself.tableView reloadData];
        };
        [weakself presentViewController:vc animated:NO completion:nil];
    }else if ([title isEqualToString:LS(@"清除总预算")]){
        [CCHud msg:LS(@"清除中")];
        [BudgetTool delete:self.model complete:^(BOOL success) {
            [NotifyTool post:notifyBudgetUpdate :nil];
            [self backBtnAction];
        }];
    }
}
- (void)loadData{
    [BudgetTool find:APP.user.account stamp:self.stamp complete:^(Budget * _Nonnull budget) {
        self.model = budget;
        [PrepayTool find:budget complete:^(NSMutableArray<Prepay *> * _Nonnull result) {
            self.model.array = result;
            CGFloat  waitpay = 0;
            CGFloat pay = 0;
            for (Prepay *model in result) {
                waitpay = model.payed ? waitpay : waitpay + model.money;
                pay = model.payed ? pay + model.money : pay;
            }
            self.model.waitPayed = waitpay;
            self.model.payed = pay;
            [self.tableView reloadData];
        }];
    }];
}
- (void)setStamp:(NSInteger )stamp{
    _stamp = stamp;
    self.date = [NSDate dateOfStamp:stamp];
    [self.timeBtn setTitle:[NSString stringWithFormat:@"%d-%02d",(int)self.date.year,(int)self.date.month] forState:UIControlStateNormal];
    [self loadData];
}

- (void)addNotify{
    [super addNotify];
    [NotifyTool add:self :@selector(loadData) :notifyPayUpdate :nil];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.array.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        BudgetCell *cell = [tableView dequeueReusableCellWithIdentifier:[BudgetCell ID]];
        cell.block = ^(NSString * _Nonnull title) {
            [self presend:title];
        };
        cell.model = self.model;
        return  cell;
    }else{
        PayCell *cell = [tableView dequeueReusableCellWithIdentifier:[PayCell ID]];
        cell.model = self.model.array[indexPath.row - 1];
        cell.selectLeft.constant = -22;
        return  cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end

